﻿using LY.Common;
using LY.Common.Utils;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.Jsons;
using LY.Entity.PageSearch;
using LY.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using LY.Entity.SearchEntity;
using QJ.CMS.Filters;
using LY.Common.Utils.Sign;
using LY.Common.Utils.Sf_Struct;
using System.Web.Script.Serialization;

namespace QJ.CMS.Controllers
{
    public class DefaultController : Controller
    {
        //
        // GET: /Default/
        private IMemberService _memberService;
        private IProjectService _projectService;
        private IMessagesService _messages;

        public DefaultController(IMemberService memberService, IProjectService projectService, IMessagesService messages)
        {
            _memberService = memberService;
            _projectService = projectService;
            _messages = messages;

        }
        public ActionResult Index(ProjectSearch search, FormCollection formCollection)
        {
            List<ProjectEx> projectEx = _projectService.QueryHotProject();
            //formCollection.Add("page", search.page ?? "1");
            //formCollection.Add("rp", "8");
            //formCollection.Add("c.CreateTime", "desc");
            //PageView view = new PageView(formCollection);
            //search.ArticleClassId = "210";
            //PagedList<ProjectEx> projectEx = _projectService.QueryHotProject(search, view);
            ViewData.Model = projectEx;

            return View();
        }

        public ActionResult List(ProjectSearch search, FormCollection formCollection)
        {
            try
            {
                formCollection.Add("page", search.page ?? "1");
                formCollection.Add("rp", "15");

                formCollection.Add("sortorder", "desc");
                //排序 zhtj
                switch (search.sort)
                {
                    case "zxsx"://最新上线
                        formCollection.Add("sortname", "Id");
                        break;
                    case "zhtj"://综合推荐
                        formCollection.Add("sortname", "zan");
                        break;
                    case "jezg"://金额最多
                        formCollection.Add("sortname", "CurrentAmount");
                        break;
                    case "zczd"://支持最多
                        formCollection.Add("sortname", "zc");
                        break;
                    case "jjjs"://即将结束
                        formCollection.Add("sortname", "strRemainDays");
                        formCollection.Set("sortorder", "asc");
                        break;
                    default:
                        goto case "zhtj";
                }


                PageView view = new PageView(formCollection);
                //项目分类的数据
                List<R_ProjectClass> projectClass = _projectService.GetProjectClassListModel();
                ViewBag.ProjectClass = projectClass;
                //项目列表
                PagedList<ProjectEx> projectEx = _projectService.QueryProject_EXList(search, view);
                ViewBag.Project_EX = projectEx;

                ViewData.Model = projectEx;

            }
            catch (Exception ex)
            {
                return RedirectToRoute("Error");
            }
            return View();
        }

        public ActionResult List1(ProjectSearch search, FormCollection formCollection)
        {
            try
            {
                formCollection.Add("page", search.page ?? "1");
                formCollection.Add("rp", "15");
                formCollection.Add("AuditStatus", search.AuditStatus);


                formCollection.Add("sortorder", "desc");
                //排序 zhtj
                switch (search.sort)
                {
                    case "zxsx"://最新上线
                        formCollection.Add("sortname", "Id");
                        break;
                    case "zhtj"://综合推荐
                        formCollection.Add("sortname", "zan");
                        break;
                    case "jezg"://金额最多
                        formCollection.Add("sortname", "CurrentAmount");
                        break;
                    case "zczd"://支持最多
                        formCollection.Add("sortname", "zc");
                        break;
                    case "jjjs"://即将结束
                        formCollection.Add("sortname", "strRemainDays");
                        formCollection.Set("sortorder", "asc");
                        break;
                    default:
                        goto case "zhtj";
                }


                PageView view = new PageView(formCollection);
                //项目分类的数据
                List<R_ProjectClass> projectClass = _projectService.GetProjectClassListModel();
                ViewBag.ProjectClass = projectClass;
                //项目列表
                // search.AuditStatus = "210";
                PagedList<ProjectEx> projectEx = _projectService.QueryProject_EXList(search, view);
                ViewBag.Project_EX = projectEx;

                ViewData.Model = projectEx;

                //热门众筹
                //formCollection = new FormCollection();
                //ProjectSearch search2 = new ProjectSearch();
                //formCollection.Add("page", search2.page ?? "1");
                //formCollection.Add("rp", "8");
                //formCollection.Add("c.CreateTime", "desc");
                //PageView views = new PageView(formCollection);
                //search2.ArticleClassId = "210";
                //PagedList<ProjectEx> projectHot = _projectService.QueryHotProject(search2, views);
                //ViewBag.Project_Hot = projectHot;

                List<ProjectEx> projectHot = _projectService.QueryHotProject();
                ViewBag.Project_Hot = projectHot;
            }
            catch (Exception ex)
            {
                return RedirectToRoute("Error");
            }
            return View();
        }

        public ActionResult Detail(int Id)
        {
            try
            {
                //点击关注的时候获取会员ID
                U_Member NowMember = null;
                if (Session["CurrentUser"] != null)
                {
                    NowMember = (U_Member)Session["CurrentUser"];
                    ViewBag.nowMemberID = NowMember.ID;
                }
                else
                {
                    ViewBag.nowMemberID = 0;
                }
                //先查询到某个具体的众筹商品
                R_Project_EX project = _projectService.GetProject_EX(Id);
                int memberId = (int)project.MemberId;
                //获得分类信息
                int projectClassId = project.ProjectClass;
                R_ProjectClass projectClass = _projectService.GetModel(projectClassId);
                //再获取发布者的信息
                Member_EX member = _memberService.GetMember(memberId);
                //获得该项目一共募集的资金
                decimal allAmount = _projectService.GetAllAmount(project.Id);
                //查询某人一共发布了几个项目
                int projectCount = _projectService.GetProjectCount(memberId);
                //最后获得档位的投资信息
                List<R_ProjectOrder_EX> list = _projectService.QueryInvestmentByItem(Id);
                //最近投资的人员
                List<U_Member_EX> memberList = _projectService.QueryInvestmentMember(project.Id);
                //获取项目进展
                IList<R_ProjectDatelinetree> projectDatelinetrees = _projectService.QueryProjectDatelinetree(project.Id);

                if (member == null) { member = new Member_EX(); }
                ViewBag.Project = project;
                ViewBag.Member = member;
                ViewBag.InvestmentList = list;
                ViewBag.ProjectClass = projectClass;
                ViewBag.AllAmount = allAmount;
                ViewBag.ProjectCount = projectCount;
                ViewBag.MemberList = memberList;
                ViewBag.projectDatelinetrees = projectDatelinetrees;
            }
            catch (Exception ex)
            {
                return RedirectToRoute("Error");
            }
            return View();
        }
        /*//分页展示全部投资的人
        [HttpPost]
        public ActionResult getTouZiPeople(FormCollection form, int ProjectId)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<U_Member_EX> plist = _projectService.QueryInvestmentMemberList(view, ProjectId);
            JsonQTable at = JsonQTable.ConvertFromPagedList<U_Member_EX>(plist, colkey, colsinfo.Split(','));
            return Json(at);
        }*/


        public ActionResult Detail1(int Id)
        {
            try
            {
                //点击关注的时候获取会员ID
                U_Member NowMember = null;
                if (Session["CurrentUser"] != null)
                {
                    NowMember = (U_Member)Session["CurrentUser"];
                    ViewBag.nowMemberID = NowMember.ID;
                }
                else
                {
                    ViewBag.nowMemberID = 0;
                }
                //先查询到某个具体的众筹商品
                R_Project_EX project = _projectService.GetProject_EX(Id);
                int memberId = (int)project.MemberId;
                //获得分类信息
                int projectClassId = project.ProjectClass;
                R_ProjectClass projectClass = _projectService.GetModel(projectClassId);
                //再获取发布者的信息
                U_Member member = _memberService.GetMember(memberId);
                //获得该项目一共募集的资金
                decimal allAmount = _projectService.GetAllAmount(project.Id);
                //查询某人一共发布了几个项目
                int projectCount = _projectService.GetProjectCount(memberId);
                //最后获得档位的投资信息
                List<R_ProjectOrder_EX> list = _projectService.QueryInvestmentByItem(Id);
                //最近投资的人员
                List<U_Member_EX> memberList = _projectService.QueryInvestmentMember(project.Id);

                ViewBag.Project = project;
                ViewBag.Member = member;
                ViewBag.InvestmentList = list;
                ViewBag.ProjectClass = projectClass;
                ViewBag.AllAmount = allAmount;
                ViewBag.ProjectCount = projectCount;
                ViewBag.MemberList = memberList;
            }
            catch (Exception ex)
            {
                return RedirectToRoute("Error");
            }
            return View();
        }

        public ActionResult Error(string errorMessage)
        {
            //if (TempData[RBSKeys.Error_Message] != null)
            //{
            //    ViewData[RBSKeys.Error_Message] = TempData[RBSKeys.Error_Message];
            //    return Content(TempData[RBSKeys.Error_Message].ToString());
            //}
            //if (ViewData[RBSKeys.Error_Message] != null)
            //{
            //    return Content(ViewData[RBSKeys.Error_Message].ToString());
            //}
            //return Content("404");
            return View();
        }
        //验证邮箱成功界面
        public ActionResult Success()
        {
            return View();
        }

        #region 私信

        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult GetValidateCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(5);
            Session["ValidateCode"] = code;
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }
        //点击发送
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult SendMessage()
        {
            JsonSMsg rMsg = new JsonSMsg();
            //判断用户是否已经登陆
            if (Session["CurrentUser"] != null)
            {
                //获取验证码
                var validateCode = Request.Form["validateCode"];
                if (validateCode != Session["ValidateCode"].ToString())
                {
                    rMsg.Status = -1;
                    rMsg.Message = "验证码错误";
                }
                else
                {
                    try
                    {
                        //获取当前登陆的会员
                        U_Member NowMember = (U_Member)Session["CurrentUser"];
                        U_Messages messages = new U_Messages();
                        messages.MessageMemberId = NowMember.ID;
                        messages.MemberId = Convert.ToInt32(Request.Form["MemberId"]);
                        messages.Message = Request.Form["Message"];
                        messages.AddTime = DateTime.Now;
                        int ret = _messages.SaveMessages(messages);
                        if (ret > 0)
                        {
                            rMsg.Status = 1;
                            rMsg.Message = "发送成功！";
                        }
                        else
                        {
                            rMsg.Status = -1;
                            rMsg.Message = "意外错误，请稍后重试！";
                        }
                    }
                    catch (Exception ex)
                    {
                        rMsg.Status = -1;
                        rMsg.Message = ex.Message;
                    }
                }

            }
            else
            {
                rMsg.Status = -2;
                rMsg.Message = "请先登陆";
            }
            return Json(rMsg);
        }
        #endregion

    }
}
